import { Base } from 'base.js';
import { Config } from 'config.js';

class Address extends Base {
  constructor() {
    super();
  }

  /**
   * 获得自己的收货地址
   */
  getAddress(callback) {
    var that = this;
    var params = {
      url: 'address',
      sCallBack: function (res) {
        if (res) {
          res.totalDetail = that.setAddressInfo(res);
          callback && callback(res);
        }
      }
    };
    this.request(params);
  }

  /**
   * 整理地址
   */
  _setUpAddress(res){
    var formData = {
      name:res.userName,
      province:res.provinceName,
      city:res.cityName,
      country:res.countyName,
      mobile:res.telNumber,
      detail:res.detailInfo
    };
    return formData;
  }

  /**
   * 创建或编辑地址
   */
  submitAddress(data,callback){
    data = this._setUpAddress(data);
    var params = {
      url:'address',
      type:'post',
      data:data,
      sCallBack:function(res){
        callback && callback(true);
      },
      eCallBack:function(res){
        callback && callback(false);
      }
    };
    this.request(params);
  }

  /**
   * 是否为直辖市
   */
  isCenterCity(name) {
    var centerCitys = ['北京市', '天津市', '上海市', '重庆市'],
      flag = centerCitys.indexOf(name) >= 0;
  }

  setAddressInfo(res) {
    var province = res.provinceName || res.province,
      city = res.cityName || res.city,
      country = res.countyName || res.country,
      detail = res.detailInfo || res.detail;
    var totalDetail = city + country + detail;

    console.log(res);

    // 直辖市，去掉详细地区
    if (!this.isCenterCity(province)) {
      totalDetail = province + totalDetail;
    }

    return totalDetail;
  }
}

export { Address }
